package me.yushuai.wukong.base.controller;

import me.yushuai.wukong.base.annotation.ActionLog;
import me.yushuai.wukong.base.api.ConfApi;
import me.yushuai.wukong.base.api.dto.*;
import me.yushuai.wukong.base.api.vo.ConfGroupVO;
import me.yushuai.wukong.base.api.vo.ConfVO;
import me.yushuai.wukong.base.convertor.ConfConvertor;
import me.yushuai.wukong.base.service.ConfService;
import me.yushuai.wukong.common.dto.PageDTO;
import me.yushuai.wukong.common.exceptin.DuplicateBizIdException;
import me.yushuai.wukong.common.exceptin.NotExistException;
import me.yushuai.wukong.common.mvc.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 配置管理，配置管理中包括了配置组、配置项管理。
 *
 * @author zhoushuai@189.cn
 * @since 2022-09-13
 */
@RestController
public class ConfController extends BaseController implements ConfApi {

    /**
     * 配置Id不存在
     */
    public static final String CONF_ID_NOT_FOUND = "Conf.id.notFound";

    /**
     * 配置项业务类
     */
    private final ConfService confService;

    private final ConfConvertor confConvertor;

    public ConfController(ConfService confService, ConfConvertor confConvertor) {
        this.confService = confService;
        this.confConvertor = confConvertor;
    }

    @Override
    @ActionLog(value = "create a conf of conf group.", returnValue = true, writeParams = true)
    public Long createConf(Long groupId, CreateConfDTO dto) throws DuplicateBizIdException, NotExistException {
        dto.setId(groupId);
        return this.confService.createConf(dto);
    }

    @Override
    @ActionLog(value = "change a conf of conf group.", returnValue = true, writeParams = true)
    public Long editConf(Long groupId, Long confId, UpdateConfValueDTO updateConfItemDTO) throws NotExistException {
        updateConfItemDTO.setGroupId(groupId);
        updateConfItemDTO.setId(confId);
        return this.confService.updateConf(updateConfItemDTO);
    }

    @Override
    @ActionLog(value = "delete a conf of conf group.", returnValue = true, writeParams = true)
    public Long deleteConf(Long groupId, Long confId) throws NotExistException {
        this.confService.deleteConf(groupId, confId);
        return confId;
    }

    @Override
    @ActionLog(value = "get a conf of conf group.", returnValue = true, writeParams = true)
    public ConfVO getConfGroupById(Long groupId, Long confId) throws NotExistException {
        ConfDTO conf = this.confService.findConfById(groupId, confId);
        return this.confConvertor.mapToConfVo(conf);
    }

    @Override
    @ActionLog(value = "paging search conf list of conf group", returnValue = true, writeParams = true)
    public PageDTO<ConfVO> pagingSearchConf(Long groupId, PagingSearchConf pagingSearch) {
        PageDTO<ConfDTO> confItemPage = this.confService.pagingSearchConf(groupId, pagingSearch);
        List<ConfVO> confItemList = confItemPage.getData().stream().map(v -> {
            final ConfVO vo = new ConfVO();
            BeanUtils.copyProperties(v, vo);
            return vo;
        }).collect(Collectors.toList());
        return PageDTO.of(pagingSearch, confItemList);
    }

    @Override
    @ActionLog(value = "create a conf group.", returnValue = true, writeParams = true)
    public Long createConfGroup(CreateConfGroupDTO dto) throws DuplicateBizIdException {
        return this.confService.createConfGroup(dto);
    }

    @Override
    @ActionLog(value = "change a conf group.", returnValue = true, writeParams = true)
    public Long editConfGroup(Long groupId, UpdateConfGroupDTO dto) throws NotExistException {
        dto.setGroupId(groupId);
        return this.confService.updateConfGroup(dto);
    }

    @Override
    @ActionLog(value = "delete a conf group.", returnValue = true, writeParams = true)
    public Long deleteConfGroup(Long groupId) throws NotExistException {
        this.confService.deleteConfGroupById(groupId);
        return groupId;
    }

    @Override
    @ActionLog(value = "get conf group details.", returnValue = true, writeParams = true)
    public ConfGroupVO getConfGroupById(Long groupId) throws NotExistException {
        final ConfGroupDTO confGroup = this.confService.findConfGroupById(groupId);
        return this.confConvertor.mapToConfGroupVo(confGroup);
    }

    @Override
    @ActionLog(value = "paging search conf of conf group.", returnValue = true, writeParams = true)
    public PageDTO<ConfGroupVO> pagingSearchConfGroup(PagingSearchConfGroup pagingSearch) {
        PageDTO<ConfGroupDTO> confGroupPage = this.confService.pagingSearchConfGroup(pagingSearch);
        List<ConfGroupVO> confProfileList = this.confConvertor.mapToConfGroupVoList(confGroupPage.getData());
        return PageDTO.of(pagingSearch, confProfileList);
    }

    @Override
    @ActionLog(value = "exits by id of conf group.", returnValue = true, writeParams = true)
    public boolean existsByConfId(Long customerTypeConfId, Long customerTypeId) {
        return false;
    }

}
